from ctypes import *

gobject = CDLL('libgobject-2.0.so')
try:
    webkit = CDLL("libwebkit-1.0.so")
except OSError:
    webkit = CDLL("libwebkit-1.0.so.2")

try:
    gtk = CDLL('libgtk-x11-2.0.so')
except OSError:
    gtk = CDLL('libgtk-x11-2.0.so.0')

try:
    gdk = CDLL('libgdk-x11-2.0.so')
except OSError:
    gdk = CDLL('libgdk-x11-2.0.so.0')

import wx

class WKHtmlWindow(wx.Window):
    def __init__(self, *args, **kwargs):
        wx.Window.__init__(self, *args, **kwargs)

        # Here is the magic to "integrate" webkit into wxPython
        # As far as I understand a wx.Window is composed of a GtkPizza
        # inside a GtkScrolledWindow 
        # (wx.Window -> GtkScrolledWindow -> GtkPizz)
        whdl = self.GetHandle()

        window = gdk.gdk_window_lookup(whdl)

        data = c_void_p()
        gdk.gdk_window_get_user_data(window, byref(data))
        pizza = data.value

        scrl = gtk.gtk_widget_get_parent(pizza)

        # Removing pizza to put a webview in it's place
        # but first we must keep a reference of "pizza"
        # to avoid some gtk warnings
        gobject.g_object_ref(pizza)
        gtk.gtk_container_remove(scrl, pizza)

        self.ctrl = ctrl = webkit.webkit_web_view_new()
        gtk.gtk_container_add(scrl, ctrl)

        gtk.gtk_widget_show_all(scrl)

    def LoadURL(self, url):
        webkit.webkit_web_view_load_uri(self.ctrl, c_char_p(url))
    LoadUrl = LoadURL

    def GetPageURL(self):
        webkit.webkit_web_view_get_uri.restype = c_char_p
        return webkit.webkit_web_view_get_uri(self.ctrl)

    def GetPageTitle(self):
        webkit.webkit_web_view_get_title.restype = c_char_p
        return webkit.webkit_web_view_get_title(self.ctrl)

    #def GetPageSource(self):
    #    #webkit.webkit_web_view_get_main_frame.restype = c_char_p
    #    frame = webkit.webkit_web_view_get_main_frame(self.ctrl)
    #    print frame
    #    #ctx = jswebkit.JSContext (frame.get_global_context ())
    #    #text = ctx.EvaluateScript ("document.documentElement.innerHTML")
    #    #return text

    #    webkit.webkit_web_view_set_view_source_mode(self.ctrl, True)

    def Reload(self):
        webkit.webkit_web_view_reload(self.ctrl)

    def RunScript(self, javascript):
        webkit.webkit_web_view_execute_script(self.ctrl, javascript)

    def IsEditable(self):
        return bool(webkit.webkit_web_view_get_editable(self.ctrl))

    def MakeEditable(self, enable):
        webkit.webkit_web_view_set_editable(self.ctrl, c_bool(enable))
    SetEditable = MakeEditable

    def CanGoBack(self):
        return bool(webkit.webkit_web_view_can_go_back(self.ctrl))

    def CanGoForward(self):
        return bool(webkit.webkit_web_view_can_go_forward(self.ctrl))

if __name__ == '__main__':
    def title():
        #print wk.GetPageTitle()
        #wk.GetPageSource()
        #wk.Reload()
        print wk.GetPageURL()
        print wk.IsEditable()
        wk.MakeEditable(True)
        print wk.IsEditable()
        print 'CanGoBack', wk.CanGoBack()
        print 'CanGoForward', wk.CanGoForward()
        wk.RunScript('alert("test");')

    app = wx.App()
    f = wx.Frame(None)
    f.Show()
    wk = WKHtmlWindow(f)
    wk.LoadURL('http://www.google.com')
    wx.FutureCall(3000, title)
    app.MainLoop()
